/*
 * _leave.java
 *
 * Created on 14 Март 2008 г., 13:31
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package xmppbot.funcs;

import java.sql.ResultSet;
import java.sql.SQLException;
import xmppbot.JJConnect;
import xmppbot .Main;
import xmppbot.db;
import xmppbot.parse.options;
import xmppbot.parse.utils;

/**
 *
 * @author lian
 */
public class _leave implements func {
    
    /**
     * Creates a new instance of _leave
     */

    
    public String process(options opts) throws SQLException {
        String ansver = null;
        ResultSet rs = null;
        String conference = null;
        String[] SplitMsg = opts.getSplitMsg();
        int nextArg = opts.getNextArg();
        if (SplitMsg.length == nextArg) {
            opts.getMchat().leave();
            ansver = "bye";
        } else {
            try {
                rs = opts.MySQLDB.getStatement().executeQuery("SELECT * FROM conferences WHERE psevdonim = '" + utils.parseSymbols(SplitMsg[nextArg]) + "';");
            } catch (SQLException ex) {
                ex.printStackTrace();
                db.killrs(rs); rs = null;
            }
            try {
                if (rs.next()) {
                    conference = rs.getString("conference") + "/" + rs.getString("login");
                    ResultSet rs2 = opts.MySQLDB.getStatement().executeQuery("SELECT * FROM servers WHERE id = " + rs.getInt("server") + ";");
                    if (rs2.next()) {
                        String server_name = rs2.getString("user") + "@" + rs2.getString("server") + "/" + rs2.getString("resource");
                        JJConnect server = Main.Connects.get(server_name);
                        server.leave(conference);
                        ansver = "Disconnect";
                    }
                    db.killrs(rs2); rs2 = null;
                } else {
                    ansver = "Bad psevdonim";
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            db.killrs(rs); rs = null;
        }
        return ansver;
    }
    
    
    public String processnotallowed(options opts) {
        return null;
    }
}
